<?php
/**
 * @author jonathan <jo.link.noir@gmail.com>
 * @version 1.0: jo_link_noir
 * @date 14-04-2010
 */

/**
 * Classe de non attaque dans le déplacement d'un armée.
 * $orderName = 'transport' : transport de ressource
 * $orderName = 'base' : transport de ressource et de vaisseaux
 * $orderName = 'colonize' : transport de ressource et de vaisseaux plus colonisation d'une planète
 */
class U1_Move_Transport extends SW_Move_Transport
{
	/** @var string*/
	public $orderName = 'transport';


	/**
	 * @param int $endPoint
	 * @param SW_Removable $removable = null
	 * @param int $time = null
	 */
	public function __construct($endPoint, SW_Removable $removable = null, $time = null)
	{
		parent::__construct($endPoint, null, null, $time);
		if ($removable)
			$this->addRemovable($removable);
	}

	/**
	 * @param SW_Units $units = null
	 * @param bool $colonize = false : ordre de colonisation
	 */
	private function sendMessage(SW_Units $units = null, $colonize = false)
	{
		$message = U1_Display_Move::getReportTransport(
			U1_DAO_Planet::selectId($this->startPoint),
			U1_DAO_Planet::selectId($this->endPoint),
			$units,
			isset($this->removables['U1_Removable_Resource'])
				? $this->removables['U1_Removable_Resource']
				: null,
			$colonize
		);

		if (!$colonize && $this->player_sends !== $this->player_target)
			U1_DAO_Message::send($this->player_target, $message, U1_Display_Move::getSujetMove($this->orderName, true), false);
		U1_DAO_Message::send($this->player_sends, $message, U1_Display_Move::getSujetMove($this->orderName, false), false);
	}

	/**
	 * Méthode à déclencher quand l'armé est arrivé à destination
	 * @param SW_Units $units
	 */
	public function arrive(SW_Units $units)
	{
		$baseSend = true;
		//récupère l'id de la planète
		if ($planetEnd = U1_DAO_Planet::selectId($this->endPoint))
		{
			if ($planetEnd->getRefUser())
			{
				if ('base' === $this->orderName)
				{
					U1_Army_DAO::getFactory()->increase($this->endPoint, $units);
					$baseSend = false;
					$this->sendMessage($units);
				}
				else
				{
					$this->depose($units);
					$this->sendMessage(null);
				}
			}
			else if ('colonize' === $this->orderName)
			{
				$unit13 = $units->offsetExists(13);
				$unit14 = $units->offsetExists(14);
				if ($unit13 || $unit14)
				{
					$planet = U1_Planet::factoryContainsPlanetId($this->startPoint);
					$countPlanet = $planet->count();
					if ($countPlanet >= 13)
					{
						if ($countPlanet < 21 && $unit14)
						{
							$unit13 = false;
							$baseSend = false;
						}
					}
					else
						$baseSend = false;

					if (!$baseSend)
					{
						//player_target est initialisé dans le DAO
						if (U1_DAO_Planet::update($this->endPoint, $this->player_sends))
						{
							$units->subtractUnit($unit13 ? 13 : 14, 1);
							//ajoute la planète
							$planet->add(U1_DAO_Planet::selectId($this->endPoint));
							//crée les ressource car non fait dans l'objet removable
							$planet->getInstance(U1_Resource::INDEX, $this->endPoint);
							U1_Army_DAO::getFactory()->increase($this->endPoint, $units);

							$this->sendMessage($units, true);
						}
						else
							$baseSend = true;
					}
				}
			}
		}

		//retour à la base
		if ($baseSend)
			U1_DAO_Move::baseSend($units, $this);
		else
		{
			$this->depose($units);
			U1_DAO_Move::deleteId($this->id);
		}
	}
}